package com.scs.application.modules.report.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.base.util.CommonService;
import com.scs.application.modules.report.entity.Config;
import com.scs.application.modules.report.service.ConfigService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;


@RestController
@RequestMapping("/report/config")
public class ConfigController extends CrudController<ConfigService, Config> {


    /**
     * 通过报表编码加载数据
     *
     * @param reportKey 报表编码
     * @param params
     * @return
     */
    @GetMapping("/fetchData")
    public RestResult fetchDataByCode(@RequestParam("reportKey") String reportKey, @RequestParam(required = false) Map<String, Object> params) throws Exception {
        Config config = service.getOne(Wrappers.<Config>query().eq("code", reportKey));
        if (config == null) {
            return RestResult.error("查询失败，可能没有此报表配置信息");
        }

        Map<String, Object> map = service.fetchData(config.getId(), params);
        return RestResult.ok("查询成功", map);
    }

    /**
     * 获取表格头文件
     * @param reportKey
     * @param params
     * @return
     */
    @GetMapping("/getGridFields")
    public RestResult getGridFields(@RequestParam("reportKey") String reportKey, @RequestParam(required = false) Map<String, Object> params) {
        Config config = service.getOne(Wrappers.<Config>query().eq("code", reportKey).last("limit 1"));
        if (config == null) {
            return RestResult.error("查询失败，可能没有此报表配置信息");
        }
        return RestResult.ok("查询成功", service.getGridFields(config.getId()));
    }

/*
    @PostMapping("/sql_parse")
    public RestResult sqlParse(@RequestBody SqlParseDTO dto) {
        List<Map<String, Object>> mapList = service.sqlParse(dto.getSql(), dto.getDatasourceId(), dto.getParams());
        return RestResult.ok("解析成功", mapList);
    }*/

    @GetMapping("/copy")
    public RestResult copy(String id,String codeNew) {
        return RestResult.ok("复制成功", service.copy(id,codeNew));
    }

    /**
     * 自动填充表格列
     * @param config
     * @return
     */
    @PostMapping("/autoGridFields")
    public RestResult autoGridFields(@RequestBody @Valid Config config) throws Exception {
        service.autoGridFields(config);
        CommonService.cachereport(config.getId());
        return RestResult.ok("更新成功");
    }

    /**
     * 通过报表编码加载数据
     *
     * @param reportKey 报表编码
     * @param params
     * @return
     */
    @GetMapping("/fetchDataForPrint")
    public RestResult fetchDataForPrint(@RequestParam("reportKey") String reportKey, @RequestParam(required = false) Map<String, Object> params) throws Exception {
        Config config = service.getOne(Wrappers.<Config>query().eq("code", reportKey).last("limit 1"));
        if (config == null) {
            return RestResult.error("查询失败，可能没有此报表配置信息");
        }

        Map<String, Object> map = service.fetchDataForPrint(config.getId(), params);
        return RestResult.ok("查询成功", map);
    }

    /**
     * 添加/更新数据
     */
    @PostMapping("/save")
    @Override
    public RestResult saveItem(@Valid @RequestBody Config entity) {
        boolean isOk = service.saveOrUpdate(entity);
        if (!isOk) {
            return RestResult.error("数据保存失败");
        }
        CommonService.cachereport(entity.getId());
        return RestResult.ok("数据保存成功", entity);
    }
}
